package com.ezlearning.platform.controller;

import com.ezlearning.platform.dto.StudentsDto;
import com.ezlearning.platform.model.Students;
import com.ezlearning.platform.repositories.StudentsRepository;
import com.ezlearning.platform.services.core.impl.StudentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
@RequestMapping("/students")
public class StudentController {

    private StudentsService studentsService;
    private StudentsRepository studentsRepository;

    @Autowired
    public StudentController(StudentsService studentsService, StudentsRepository studentsRepository) {
        this.studentsService = studentsService;
        this.studentsRepository = studentsRepository;
    }

    @GetMapping("/add")
    public String addStudents(Model model) {
        model.addAttribute("student", new StudentsDto());
        return "students/student-add";
    }

    @PostMapping("/save")
    public String saveStudents(StudentsDto students) {
        studentsService.create(students);
        return "redirect:/students";
    }

    @GetMapping("/edit/{id_students}")
    public String getStudentsForUpdate(@PathVariable Long id_students, Model model) {
        try {
            Students studentsActual = studentsRepository.findById(id_students).get();
            model.addAttribute("student", studentsActual);
            return "students/student-edit";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error", e);
            return "error";
        }
    }

    @PostMapping("/update/{id_students}")
    public String updateStudents(@PathVariable Long id_students,
                                 Students students, RedirectAttributes attributes, Model model) {
        try {
            Students currentStudents = studentsRepository.findById(id_students).get();
            currentStudents.setNomStudents(students.getNomStudents());
            currentStudents.setApeStudents(students.getApeStudents());
            currentStudents.setCorreoStudents(students.getCorreoStudents());
            currentStudents.setDescStudents(students.getDescStudents());
            currentStudents.setImgurl(students.getImgurl());

            studentsService.update(students);
            attributes.addAttribute("id_students", id_students);

            return "redirect:/students/{id_students}";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error", e);
            return "error";
        }
    }

    @GetMapping
    public String getStudentsList(Model model) {
        List<Students> studentsList = studentsService.getAll();
        model.addAttribute("students", studentsList);
        return "students/students";
    }

    @GetMapping("/delete/{id_students}")
    public String deleteStudents(@PathVariable Long id_students, Model model) {
        try {
            Students studentsActual = studentsRepository.findById(id_students).get();
            studentsService.delete(studentsActual);
            return "redirect:/students";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error", e);
            return "error";
        }
    }

    @GetMapping("/{id_students}")
    public String getStudentsDetail(@PathVariable Long id_students, Model model) {
        try {
            Students students = studentsRepository.findById(id_students).get();
            model.addAttribute("student", students);
            return "students/student-detail";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error", e);
            return "error";
        }
    }
}